package cn.skyquant.quant4j.api.system;


import cn.skyquant.quant4j.sdk.util.constant.EAConstants;
import com.sun.management.OperatingSystemMXBean;

import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

public interface SystemService {

    int expireTime = 2;
    TimeUnit timeUnit = TimeUnit.MINUTES;

    String name = "name";
    String startTime = "startTime";
    String updatedTime = "updatedTime";
    String server_ip = "server_ip";
    String server_hostname = "server_hostname";

    String jvm_total = "jvm_total";
    String jvm_free = "jvm_free";
    String jvm_max = "jvm_max";
    String jvm_version = "jvm_version";

    String os_max = "os_max";
    String os_free = "os_free";
    String os_name = "os_name";

    Map emptyMap = new HashMap();

    default Map fullInfo() {
        Map map = new HashMap<>();
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {

        }
        if (inetAddress != null) {
            map.put(server_ip, inetAddress.getHostAddress());
            map.put(server_hostname, inetAddress.getHostName());
        }
        Runtime runtime = Runtime.getRuntime();
        map.put(name, getName());
        map.put(startTime, System.currentTimeMillis());
        map.put(updatedTime, System.currentTimeMillis());
        map.put(jvm_total, runtime.totalMemory());
        map.put(jvm_free, runtime.freeMemory());
        map.put(jvm_max, runtime.maxMemory());
        map.put(jvm_version, System.getProperty("java.version") + "_" + System.getProperty("sun.arch.data.model"));
        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        String osname = System.getProperty("os.name");
        map.put(os_max, osmxb.getTotalPhysicalMemorySize());
        map.put(os_free, osmxb.getFreePhysicalMemorySize());
        map.put(os_name, osname);
        return map;
    }


    default Map changedInfo() {
        Map map = new HashMap<>();

        Runtime runtime = Runtime.getRuntime();
        map.put(updatedTime, System.currentTimeMillis());
        map.put(jvm_total, runtime.totalMemory());
        map.put(jvm_free, runtime.freeMemory());
        map.put(jvm_max, runtime.maxMemory());

        OperatingSystemMXBean osmxb = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean();
        String osname = System.getProperty("os.name");
        map.put(os_max, osmxb.getTotalPhysicalMemorySize());
        map.put(os_free, osmxb.getFreePhysicalMemorySize());
        map.put(os_name, osname);
        return map;
    }


    String key_name_set = "SYSTEMINFO_KEYS";
    String SYSTEM = "SYSTEMINFO";

    String getName();

    default String getKey() {
        InetAddress inetAddress = null;
        try {
            inetAddress = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            return null;
        }
        if (inetAddress != null) {
            return SYSTEM + EAConstants.split + getName() + EAConstants.split + inetAddress.getHostName();
        } else {
            return null;
        }
    }
}
